﻿using System.Text.RegularExpressions;

namespace CommonNet48.Extend
{
    /// <summary>
    /// 字符串验证扩展类
    /// </summary>
    public static class StringValidateExtend
    {
        /// <summary>
        /// 验证输入字符串是否为数值
        /// </summary>
        /// <param name="sInputStr">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(this string sInputStr)
        {
            if (string.IsNullOrWhiteSpace(sInputStr)) return false;
            return new Regex(@"^(\d*,?(\d3)?)+.?\d*$").Match(sInputStr).Success;
        }

        /// <summary>
        /// 验证输入字符串是否为IP地址
        /// </summary>
        /// <param name="sInputStr">输入字符串</param>
        /// <returns></returns>
        public static bool IsIPAddress(this string sInputStr)
        {
            if (string.IsNullOrWhiteSpace(sInputStr)) return false;
            return new Regex(@"^(?:(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:1[0-9][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:2[0-5][0-5])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[0-9]))$").Match(sInputStr).Success;
        }

        /// <summary>
        /// 验证输入字符串是否为正整数
        /// </summary>
        /// <param name="sInputStr">输入字符串</param>
        /// <returns></returns>
        public static bool IsPositiveIntNum(this string sInputStr)
        {
            if (string.IsNullOrWhiteSpace(sInputStr)) return false;
            return new Regex(@"^[0-9]*[1-9][0-9]*$").Match(sInputStr).Success;
        }

        /// <summary>
        /// 验证是否为十六进制数
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static bool IsHexNum(this string str, int len)
        {
            if (string.IsNullOrWhiteSpace(str)) return false;
            if (str.Length != len * 2)
                return false;
            else
                return new Regex(@"[0-9a-fA-F]{" + len.ToString() + "}").Match(str).Success;
        }

        /// <summary>
        /// 验证是否为比例格式(正整数:正整数)，例："0:0"、"11:100"、"100:100"
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <returns></returns>
        public static bool IsRatioFormat(this string str)
        {
            if (string.IsNullOrEmpty(str)) return false;
            return new Regex(@"^\d+:\d+$").Match(str).Success;
        }

        /// <summary>
        /// 验证是否为数值范围（decimal~decimal），例"88.88~-666.768"
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumRange(this string str)
        {
            if (string.IsNullOrEmpty(str)) return false;
            return new Regex(@"^((\-|\+)?\d+(\.\d+)?)~((\-|\+)?\d+(\.\d+)?)$").Match(str).Success;
        }

        /// <summary>
        /// 验证是否是错误的文件夹名称
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsErrorDirectoryName(this string str)
        {
            if (string.IsNullOrEmpty(str)) return true;
            return new Regex("[\\\\/:*?\"<>|]").Match(str).Success;
        }

        /// <summary>
        /// 替换非法的文件名字符
        /// </summary>
        /// <param name="str"></param>
        /// <param name="newStr">新的字符</param>
        public static string ReplaceErrorFileNameChar(this string str, string newStr)
        {
            if (string.IsNullOrEmpty(str)) return str;
            return Regex.Replace(str, "[\\\\/:*?\"<>|]", newStr);
        }
    }
}
